<?php
namespace app\queue\redis;

use Webman\RedisQueue\Consumer; 
use support\Log;
use Webman\RedisQueue\Redis;
use Webman\RedisQueue\Client;

class OrderConsumer implements Consumer
{
    // 要消费的队列名 
    public $queue = 'order_create';
    
    // Redis连接配置
    public $connection = 'default';
    
    // 消费逻辑
    public function consume($data)
    {
        try {    
            $start_time = microtime(true);
            $code = $data['code']; 
            //秒级成交量
            $sortSetKey = "second:chengjiaoliang:".date('Y-m-d').":top";
            //秒级成交量
            $sortSetKey10000 = "second:chengjiaoliang:".date('Y-m-d').":10000:top"; 
            
            $hashCodeKey = "second:chengjiaoliang:".date('Y-m-d').":".$code;
            
            $response =  self::getBaseXueQiuCurl($code);
            Log::info("返回数据: $response");
            $data = json_decode($response,true);
          
            if($data['error_code'] == 0){
                $data = $data['data'][0];
                $result = [
                    'code' => $data['symbol'],
                    'price' => $data['current'],
                    'times' => date("Y-m-d H:i:s", $data['timestamp']/1000),
                    'trade_volume' =>  $data['trade_volume']/100,
                    'real_time' =>date("Y-m-d H:i:s")
                ];
                
                $hashKey = date("Y-m-d H:i:s", $data['timestamp']/1000);
                $times = $data['trade_volume']/100;
                $name = $code.":".date("H:i:s", $data['timestamp']/1000);
                if(!Redis::zScore($sortSetKey10000, $name) && $times >= 10000){
                    Redis::zAdd($sortSetKey10000, $times, $name);
                    //发送邮件
                    Client::send('email_send', $data);
                }  
                //记录出现万手的股票
                if(!Redis::zScore($sortSetKey, $name)){
                    Redis::zAdd($sortSetKey, $times, $name);
                }
                //hash
                if(!Redis::hExists($hashCodeKey, $hashKey)){
                    Redis::hSet($hashCodeKey, $hashKey, $times);
                }
            }
            echo date("Y-m-d H:i:s")."【order_create】-处理{$code}成功"."\r\n"; 
            Log::info("订单处理成功: $code 耗时：".(microtime(true)- $start_time));
            return true; 
        } catch (\Throwable $e) {
            Log::error("订单处理失败: {$e->getLine()}  {$e->getMessage()}");
            return false; // 触发重试
        }
    } 
    
    /**
     * 爬虫记录
     * @param unknown $code
     * @return mixed
     */
    public static function getBaseXueQiuCurl($code) {
        // 初始化
        $begin_time = (time () - rand ( 10, 50 )) . rand ( 100, 999 );
        $end_time = (time () + rand ( 10, 50 )) . rand ( 100, 999 );
        //$url = "https://api.xueqiu.com/cubes/rebalancing/history.json?cube_id=$cube_id&count=20&page=1&_t=1XIAOMIb36227dc099f78ce791b32ffdf8ff1d0.1035746805." . $begin_time . '.' . $end_time . '&_s=' . $_s;
        
        $_s ="a9c45f";
        //$url = "https://stock.xueqiu.com/v5/stock/realtime/quotec.json?symbol=SH603011&is_delay_hk=false&is_delay_futures=true&trade_session=2&_t=1XIAOMIdbdbe53579ad35347ace0e8848be9efe.1035746805.1743562707791.1743563117263&_s=a9c45f";
        //*
        /**
         * GET https://stock.xueqiu.com/v5/stock/realtime/quotec.json?symbol=SH603011&is_delay_hk=false&is_delay_futures=true&trade_session=2&_t=1XIAOMIdbdbe53579ad35347ace0e8848be9efe.1035746805.1743562707791.1743563117263&_s=a9c45f HTTP/1.1
         Accept-Language: en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4
         Cookie: xq_a_token=abfd9c65d471bdcdac59a60cf0194d3590d3285d;xq_id_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJ1aWQiOjEwMzU3NDY4MDUsImlzcyI6InVjIiwiZXhwIjoxNzQ1NTYzMjE4LCJjdG0iOjE3NDI5NzE3MjkwMjQsImNpZCI6Ikp0WGJhTW43ZVAifQ.isvglkTj-HtlzlHvHqePMP-pSWqFapRlhain8T5XlxGKUH-BsUbt235NFLC9Qz10m9olW2W_mTEz-tLajleAxJO8FugKVqpUOZoUL08fiXwDxkbj1Fr-g7GNdiO6yFIeiFBvFTvVz2H5Tjj1O0vodvzKQF7U39Ai5qhhufRptafkMYUwcnfQcToWnKxka7O5UTVl_ysRoq5bPcyRuJ63uhb3DhconxIxVQ37KODwNz1fnWEKjIKiFK9gUqd-cbkRTOJpPGWZ2M1KfpeP3FE5RhyEQB8QGBjKviYMhFW-adQXzh2aS-DX3eQVObrrVUSw25ZwKmCNy-t1W1MykYqqzw;u=1035746805;session_id=;xid=0
         X-Device-ID: 1XIAOMIdbdbe53579ad35347ace0e8848be9efe
         User-Agent: Xueqiu Android 14.15.1
         X-Device-Model-Name: HUAWEI_TAS-AN00
         X-Device-OS: Android 9
         Host: stock.xueqiu.com
         Connection: Keep-Alive
         Accept-Encoding: gzip
         
         
         * @var Ambiguous $url
         */
        
        
        
        $url = "https://stock.xueqiu.com/v5/stock/realtime/quotec.json?symbol=$code&is_delay_hk=false&is_delay_futures=true&trade_session=2&_t=1XIAOMIdbdbe53579ad35347ace0e8848be9efe.1035746805." . $begin_time . '.' . $end_time . '&_s=' . $_s;
        
        $ch = curl_init ();
        $header [] = "Host: stock.xueqiu.com";
        $header [] = "Accept-Language: en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4";
        $header [] = "Cookie: xq_a_token=abfd9c65d471bdcdac59a60cf0194d3590d3285d;xq_id_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJ1aWQiOjEwMzU3NDY4MDUsImlzcyI6InVjIiwiZXhwIjoxNzQ1NTYzMjE4LCJjdG0iOjE3NDI5NzE3MjkwMjQsImNpZCI6Ikp0WGJhTW43ZVAifQ.isvglkTj-HtlzlHvHqePMP-pSWqFapRlhain8T5XlxGKUH-BsUbt235NFLC9Qz10m9olW2W_mTEz-tLajleAxJO8FugKVqpUOZoUL08fiXwDxkbj1Fr-g7GNdiO6yFIeiFBvFTvVz2H5Tjj1O0vodvzKQF7U39Ai5qhhufRptafkMYUwcnfQcToWnKxka7O5UTVl_ysRoq5bPcyRuJ63uhb3DhconxIxVQ37KODwNz1fnWEKjIKiFK9gUqd-cbkRTOJpPGWZ2M1KfpeP3FE5RhyEQB8QGBjKviYMhFW-adQXzh2aS-DX3eQVObrrVUSw25ZwKmCNy-t1W1MykYqqzw;u=1035746805;session_id=;xid=0";
        $header [] = "X-Device-ID: 1XIAOMIdbdbe53579ad35347ace0e8848be9efe";
        $header [] = "X-Device-Model-Name: HUAWEI_TAS-AN00";
        $header [] = "X-Device-OS: Android 9";
        $header [] = "Accept-Encoding: gzip";
        $header [] = "Connection: Keep-Alive";
        
        $xforip = rand ( 1, 254 ) . "." . rand ( 1, 254 ) . "." . rand ( 1, 254 ) . "." . rand ( 1, 254 );
        $header [] = "X-FORWARDED-FOR: $xforip";
        $header [] = "CLIENT-IP: $xforip";
        curl_setopt ( $ch, CURLOPT_HTTPHEADER, $header );
        curl_setopt ( $ch, CURLOPT_ENCODING, "" );
        curl_setopt ( $ch, CURLOPT_URL, $url );
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, 1 );
        curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false );
        curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, false );
        curl_setopt ( $ch, CURLOPT_TIMEOUT, 3 ); // 设置1秒超时
        curl_setopt ( $ch, CURLOPT_USERAGENT, "Xueqiu Android 14.25" );
        $output = curl_exec ( $ch );
        // $output = mb_convert_encoding($output, 'utf-8', 'GBK,UTF-8,ASCII');
        curl_close ( $ch );
        return $output;
    }
    
    /**
     * curlGet
     * @param unknown $url
     * @return mixed
     */
    public static function curlGet($url){
        $ch = curl_init();
        $header [] = "Host: stock.xueqiu.com";
        $header [] = "Accept-Language: en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4";
        $header [] = "Cookie: xq_a_token=abfd9c65d471bdcdac59a60cf0194d3590d3285d;xq_id_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJ1aWQiOjEwMzU3NDY4MDUsImlzcyI6InVjIiwiZXhwIjoxNzQ1NTYzMjE4LCJjdG0iOjE3NDI5NzE3MjkwMjQsImNpZCI6Ikp0WGJhTW43ZVAifQ.isvglkTj-HtlzlHvHqePMP-pSWqFapRlhain8T5XlxGKUH-BsUbt235NFLC9Qz10m9olW2W_mTEz-tLajleAxJO8FugKVqpUOZoUL08fiXwDxkbj1Fr-g7GNdiO6yFIeiFBvFTvVz2H5Tjj1O0vodvzKQF7U39Ai5qhhufRptafkMYUwcnfQcToWnKxka7O5UTVl_ysRoq5bPcyRuJ63uhb3DhconxIxVQ37KODwNz1fnWEKjIKiFK9gUqd-cbkRTOJpPGWZ2M1KfpeP3FE5RhyEQB8QGBjKviYMhFW-adQXzh2aS-DX3eQVObrrVUSw25ZwKmCNy-t1W1MykYqqzw;u=1035746805;session_id=;xid=0";
        $header [] = "X-Device-ID: 1XIAOMIdbdbe53579ad35347ace0e8848be9efe";
        $header [] = "X-Device-Model-Name: HUAWEI_TAS-AN00";
        $header [] = "X-Device-OS: Android 9";
        $header [] = "Accept-Encoding: gzip";
        $header [] = "Connection: Keep-Alive";
        
        $xforip = rand ( 1, 254 ) . "." . rand ( 1, 254 ) . "." . rand ( 1, 254 ) . "." . rand ( 1, 254 );
        $header [] = "X-FORWARDED-FOR: $xforip";
        $header [] = "CLIENT-IP: $xforip";
        curl_setopt ( $ch, CURLOPT_HTTPHEADER, $header );
        curl_setopt ( $ch, CURLOPT_ENCODING, "" );
        curl_setopt ( $ch, CURLOPT_URL, $url );
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, 1 );
        curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false );
        curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, false );
        curl_setopt ( $ch, CURLOPT_TIMEOUT, 3 ); // 设置1秒超时
        curl_setopt ( $ch, CURLOPT_USERAGENT, "Xueqiu Android 14.25" );
        $output = curl_exec ( $ch );
        curl_close($ch);
        return $output;
    }
    
} 